package pack1;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.swim.entity.RegisteredUser;
import com.swim.session.RegisteredUserRemote;

public class SuggestionsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
      
    
    public SuggestionsServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	HttpSession session = request.getSession();
    	String viaEmail = request.getParameter("via");
    	if(session.getAttribute("authenticated") == null || 
    			!(boolean)session.getAttribute("authenticated") || viaEmail == null) {
    		response.sendRedirect("index.jsp");
			return;
    	}
    	showSuggestions(request, response,viaEmail);
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String viaEmail = (String) request.getAttribute("via");
    	if(request.getSession().getAttribute("authenticated") == null ||
    			!(boolean)request.getSession().getAttribute("authenticated") || viaEmail == null) {
    		response.sendRedirect("index.jsp");
			return;
    	}
    	
		showSuggestions(request, response,viaEmail);
    }
    
    private void showSuggestions(HttpServletRequest request, HttpServletResponse response,String viaEmail) throws ServletException, IOException {

    	HttpSession session = request.getSession();
    	List<RegisteredUser> suggestions = getSuggestionsList(viaEmail,(String)session.getAttribute("authenticated-email"));
		
    	request.setAttribute("suggestions", suggestions);
    	deleteNotification((String)session.getAttribute("authenticated-email"),viaEmail);
		RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/auth/suggestions.jsp");
		dispatcher.forward(request, response);
    }

	private void deleteNotification(String toEmail, String viaEmail) {
		try {
			InitialContext jndiContext = (InitialContext) Util.getInitialContext();
			Object ref = jndiContext.lookup("RegisteredUserBeanJNDI");
			RegisteredUserRemote r = (RegisteredUserRemote) ref;
			r.deleteNotification(toEmail,viaEmail);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private List<RegisteredUser> getSuggestionsList(String viaEmail, String forEmail) {
		List<RegisteredUser> results = new ArrayList<RegisteredUser>();
		try {
			InitialContext jndiContext = (InitialContext) Util.getInitialContext();
			Object ref = jndiContext.lookup("RegisteredUserBeanJNDI");
			RegisteredUserRemote r = (RegisteredUserRemote) ref;
			results = new ArrayList<RegisteredUser>();
			results.addAll(r.getSuggestions(forEmail,viaEmail));
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return results;
	}
	
}
